package com.serverapk.camera;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import com.serverapk.PostNex;
import com.serverapk.para.Common;
public class ImgChange {
	private static int with = Common.max_width;
	private static int hight = Common.max_height;
	
	public static void changImg(){
        try {
        	delFiles(Common.filepath_bak);
        	LinkedHashMap<String,String> files = getFiles(Common.filepath);
            for (String fileName : files.keySet()) {
            	Options options = new Options();
            	options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(files.get(fileName), options);
            	options.inSampleSize = calculateInSampleSize(options, with, hight);
            	options.inJustDecodeBounds = false;
            	Bitmap bitmap = BitmapFactory.decodeFile(files.get(fileName), options);  
            	
            	File file = new File(Common.filepath_bak+fileName);
            	file.createNewFile();
            	FileOutputStream out = new FileOutputStream(file);
            	bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
            }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
        
		/**
		 * 读取某个目录下所有文件、文件夹
		 * @param path
		 * @return LinkedHashMap<String,String>
		 */
		    public static LinkedHashMap<String,String> getFiles(String path) {
		        LinkedHashMap<String,String> files = new LinkedHashMap<String,String>();
		        File file = new File(path);
		        File[] tempList = file.listFiles();
		 
		        for (int i = 0; i < tempList.length; i++) {
		            if (!tempList[i].isDirectory()) {
		                files.put(tempList[i].getName(),tempList[i].getPath());
		            }
		        }
		        return files;
		    }
		    
		/**
		 * 删除文件
		 * @param path
		 */
		public static void delFiles(String path) {
		    LinkedHashMap<String,String> files = new LinkedHashMap<String,String>();
		    File file = new File(path);
		    File[] tempList = file.listFiles();
		    if(tempList != null){
		        for (int i = 0; i < tempList.length; i++) {
		            if (!tempList[i].isDirectory()) {
		            	tempList[i].delete();
		            }
		        }
		    }
		}
        

		/**
		 * 计算压缩率
		 * @param options
		 * @param reqWidth
		 * @param reqHeight
		 * @return
		 */
		public static int calculateInSampleSize(Options options, int reqWidth, int reqHeight) {
				final int height = options.outHeight;
				final int width = options.outWidth;
				int inSampleSize = 1;
				if (height > reqHeight || width > reqWidth) {
					final int heightRatio = Math.round((float) height / (float) reqHeight);
					final int widthRatio = Math.round((float) width / (float) reqWidth);
					inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
				}
				return inSampleSize;
			}
		}